Pages

Wednesday, February 3, 2021

Sorting ArrayList in Reverse or Descending Order in Java 8

1. Overview

In this article, We will learn how to sort ArrayList in descending order in java. Sometimes this is referred as collections reverse or decreasing order.

To get the list in the reverse order, we need to use Collections.reverseOrder() and Collections.sort() methods together.

Sorting ArrayList in Reverse or Descending Order in Java 8


2.  Sort ArrayList In Descending order using Collections.reverseOrder()

We have already shown how to sort list in ascending order using Collections.sort() method.

In the below examples, we are using the built-in comparator from the reverseOrder() method and passing it to the Collections.sort() method.


package com.javaprogramto.java8.arraylist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ArrayListReverseOrder1 {

	public static void main(String[] args) {

		// Creating ArrayList
		List<Integer> numbersList = new ArrayList<>();

		// Adding values to List
		numbersList.add(150);
		numbersList.add(50);
		numbersList.add(250);
		numbersList.add(500);
		numbersList.add(350);

		// printing before sorting
		System.out.println("Before sorting : " + numbersList);

		// Getting the descending order comparator
		Comparator<Integer> reverseComparator = Collections.reverseOrder();

		// Sorting with the reverse comparator with sort() method.
		// sort() method internally uses this comparator to sort in the descending order
		Collections.sort(numbersList, reverseComparator);

		// printing the final list after reverse order sorting. Original list only
		// sorted.
		System.out.println("After sorting : " + numbersList);
	}
}

 

Output:

Before sorting : [150, 50, 250, 500, 350]
After sorting : [500, 350, 250, 150, 50]
 

3.  Sort ArrayList In Descending order using Collections.reverse()

Next, look at the another way to sort the arraylist in descending order using two methods as below.

Collections.sort(arraylist); --> first sorts the list in the ascending order

Collections.reverse(arraylist); --> Next, reverse the sorted list.

Example:


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ArrayListReverseOrder2 {

	public static void main(String[] args) {

		// Creating ArrayList
		List<Integer> numbersList = new ArrayList<>();

		// Adding values to List
		numbersList.add(150);
		numbersList.add(50);
		numbersList.add(250);
		numbersList.add(500);
		numbersList.add(350);

		// printing before sorting
		System.out.println("Before sorting : " + numbersList);

		// sorting the list in the ascending order
		Collections.sort(numbersList);

		// reversing the sorted list into descending order
		Collections.reverse(numbersList);

		// printing the final list after reverse order sorting. Original list only
		// sorted.
		System.out.println("After sorting : " + numbersList);

	}

}
 

This program also produces the same output as in the section 2.

4. Java 8 Sort ArrayList Descending Order

Sorting list in reverse order is pretty easy from stream.sorted(Collections.reverseOrder()) in java 8 api.

We can use parallelStream() method to work efficiently with larger data volumes.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayListReverseOrder2 {

	public static void main(String[] args) {

		// Creating ArrayList
		List<Integer> numbersList = new ArrayList<>();

		// Adding values to List
		numbersList.add(150);
		numbersList.add(50);
		numbersList.add(250);
		numbersList.add(500);
		numbersList.add(350);

		// printing before sorting
		System.out.println("Before sorting : " + numbersList);

		List<Integer> descendingList = numbersList.stream()
				.sorted(Collections.reverseOrder())
				.collect(Collectors.toList());

		// printing the final list after reverse order sorting. Original list only
		// sorted.
		System.out.println("After sorting : " + descendingList);
	}
}
 


5. Conclusion

In this article, We have seen how to sort the ArrayList in descending order in older and new java 8 streams.

GitHub

How to sort Map by values in java 8?

No comments:

Post a Comment

Please do not add any spam links in the comments section.